﻿using Corina.Commands;
using Corina.Datamodel.DataObject;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using Corina.Utilities;
using Corina.Win.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace Corina.Win.ServerHub.Viewmodels
{
    public class LoginViewmodel : ViewmodelBase
    {
        private string _loginAccount = string.Empty;
        public string LoginAccount
        {
            get { return _loginAccount; }
            set
            {
                _loginAccount = value;
                RaisePropertyChanged(() => LoginAccount);
                RaisePropertyChanged(() => LoginCommand);
            }
        }

        private string _loginPassword = string.Empty;
        public string LoginPassword
        {
            get { return _loginPassword; }
            set
            {
                _loginPassword = value;
                RaisePropertyChanged(() => LoginPassword);
                RaisePropertyChanged(() => LoginCommand);
            }
        }

        #region Methods
        
        private void Login()
        {
            IsBusy = true;
            Task<ResultInfo> task = CoreClient.LoginAsync(LoginAccount, SecurityHelper.GetMd5(LoginPassword));
            //try
            //{
            //    task.Wait();
            //}
            //catch(Exception ex)
            //{
            //    ShowPrompt(ex.InnerException.Message);
            //    ErrorTraceHelper.Trace(ex);
            //}

            task.ContinueWith((t) => {
                if (task.IsCompleted)
                {
                    Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                        {
                            if (task.Exception == null)
                            {
                                if (task.Result.Result == ResultValue.Succeed)
                                {
                                    DataContext.CurrentAccount = task.Result.Entities.OfType<ManagerInfo>().FirstOrDefault();
                                }
                                else
                                {
                                    ShowPrompt(task.Result.Description);
                                }
                            }
                            else
                            {
                                ShowPrompt(task.Exception.InnerException.Message);
                                ErrorTraceHelper.Trace(task.Exception);
                            }
                        }));
                    IsBusy = false;
                }
            });

        }

        #endregion

        public void OnPasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox box = sender as PasswordBox;
            LoginPassword = box.Password;
        }

        public ICommand LoginCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return LoginAccount.Length > 3 && LoginPassword.Length > 6; },
                    ExecuteCallback = delegate {
                        Login();
                    }
                };
            }
        }
    }
}
